package com.biwano.swapmyjob.beans.user;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.biwano.swapmyjob.beans.BeanException;
import com.biwano.swapmyjob.dao.DBException;
import com.biwano.swapmyjob.dao.UserDao;
import com.biwano.swapmyjob.objects.User;



@SessionScoped
@ManagedBean
public class UserBean {

	private boolean authenticated;
	private User user;
	@EJB
	UserDao userDao;

	private String locale;
	
	public boolean isAuthenticated() {
		if (authenticated==false)
			try {
				load("biwano");
			} catch (BeanException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return authenticated;
	}
	public void setAuthenticated(boolean authentified) {
		this.authenticated = authentified;
	}
	public void authentify(String login, String password) throws BeanException
	{
		try{
			setUser(userDao.authentify(login,password));
			setAuthenticated(true);
		}
		catch (DBException e)
		{
			throw new BeanException();
		}
	}
	public void load(String login) throws BeanException
	{
		try{
			setUser(userDao.findEntity(login));
			setAuthenticated(true);
		}
		catch (DBException e)
		{
			throw new BeanException();
		}
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
		if (user!=null)
			setLocale(user.getLocale());
		
	}
	public String getLocale() {
		return locale;
	}
	public void setLocale(String locale) {
		this.locale = locale;
	}
	
}
